$(".login a").on("click",function(){
  $(".login").hide().next().show()
})

$(".registered a").on("click",function(){
  $(".login").show().next().hide()
})

$(".registered form").on("submit",function(e){
  e.preventDefault()
  let data = $(this).serialize()
  $.ajax({
    type:"post",
    url:"http://ajax.frontend.itheima.net/api/reguser",
    data:data,
    success:function(res){
      layer.msg(res.message)
      if(res.status === 0){
        $(".registered form")[0].reset()
        $(".login").show().next().hide()
      }
    }
  })
})

let form = layui.form

form.verify ({
  len: [/^\S{6,12}$/, '密码这么短,懒死你得了'],
  same: function (val) {
    let pwd = $('.reg-pwd').val();
    if (pwd !== val) {
        return '能把密码输对吗,笨b'
    }
}
})

//------------------------ 登录跳转 -------------------------
$(".login form").on("submit",function(e){
  e.preventDefault()
  $.ajax({
    type:"post",
    url:"http://ajax.frontend.itheima.net/api/login",
    data:$(this).serialize(),
    success:function(res){
      if(res.status === 0){
        layer.msg(res.message)
        localStorage.setItem("token",res.token)
        setTimeout(function(){
          location.href = "/index.html"
        },1500)
      }
    }
  })
})